<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>vuepress-mergeable | VuePress 社区</title>
    <meta name="generator" content="VuePress 1.4.1">
    <link rel="icon" href="/logo/600x600.png">
    <meta name="description" content="社区维护的 VuePress 生态系统">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.6c0c5975.css" as="style"><link rel="preload" href="/assets/js/app.642d23b3.js" as="script"><link rel="preload" href="/assets/js/40.20d66e6f.js" as="script"><link rel="preload" href="/assets/js/page-5b4049d8.28b01370.js" as="script"><link rel="preload" href="/assets/js/42.2c37f203.js" as="script"><link rel="prefetch" href="/assets/js/41.3a1d8c85.js"><link rel="prefetch" href="/assets/js/43.6ef7f18f.js"><link rel="prefetch" href="/assets/js/44.b27fbcf7.js"><link rel="prefetch" href="/assets/js/45.c3252ceb.js"><link rel="prefetch" href="/assets/js/46.9c844637.js"><link rel="prefetch" href="/assets/js/page-07d1601e.242c2d2a.js"><link rel="prefetch" href="/assets/js/page-07f51dda.32726b1b.js"><link rel="prefetch" href="/assets/js/page-0a6678e5.628ba2fc.js"><link rel="prefetch" href="/assets/js/page-0f8f6766.b84a099f.js"><link rel="prefetch" href="/assets/js/page-1204fa10.e7865c5b.js"><link rel="prefetch" href="/assets/js/page-12e4fd46.7452ae30.js"><link rel="prefetch" href="/assets/js/page-14660f14.f5838285.js"><link rel="prefetch" href="/assets/js/page-17a68826.0f6f2110.js"><link rel="prefetch" href="/assets/js/page-1982deb4.01bbc28c.js"><link rel="prefetch" href="/assets/js/page-216dd7c6.119f4c59.js"><link rel="prefetch" href="/assets/js/page-2d1fe5d8.ab8c6d4c.js"><link rel="prefetch" href="/assets/js/page-33083986.2f59b20d.js"><link rel="prefetch" href="/assets/js/page-353b8594.d79d4319.js"><link rel="prefetch" href="/assets/js/page-36ce40ea.66f2730d.js"><link rel="prefetch" href="/assets/js/page-3b6ebfa4.df1831eb.js"><link rel="prefetch" href="/assets/js/page-3e5179ee.f0d7043f.js"><link rel="prefetch" href="/assets/js/page-42f1e69c.ad94d467.js"><link rel="prefetch" href="/assets/js/page-4336d0c6.a4013cdd.js"><link rel="prefetch" href="/assets/js/page-4362efe0.40a6a106.js"><link rel="prefetch" href="/assets/js/page-4d80adb0.c788d5af.js"><link rel="prefetch" href="/assets/js/page-4eab9ae2.3c07cca2.js"><link rel="prefetch" href="/assets/js/page-5170c35e.ba839aac.js"><link rel="prefetch" href="/assets/js/page-55154126.4114c7df.js"><link rel="prefetch" href="/assets/js/page-5c13450a.559f8c58.js"><link rel="prefetch" href="/assets/js/page-5e11724a.743dcab4.js"><link rel="prefetch" href="/assets/js/page-6aa1c294.d5c24ca3.js"><link rel="prefetch" href="/assets/js/page-6aa62140.570cd351.js"><link rel="prefetch" href="/assets/js/page-6c372be6.1768c8fc.js"><link rel="prefetch" href="/assets/js/page-6c6f6eb8.55332c8a.js"><link rel="prefetch" href="/assets/js/page-70397cc5.38298d74.js"><link rel="prefetch" href="/assets/js/page-7a8d5874.bebc26b5.js"><link rel="prefetch" href="/assets/js/page-7bec420c.42c63b19.js"><link rel="prefetch" href="/assets/js/page-8ed296e4.ee2655fc.js"><link rel="prefetch" href="/assets/js/page-c3486808.466685fd.js"><link rel="prefetch" href="/assets/js/page-c9903bc8.3b89aecd.js"><link rel="prefetch" href="/assets/js/page-e2b999b4.9e2fd49d.js"><link rel="prefetch" href="/assets/js/page-f3f5674c.c5689d98.js">
    <link rel="stylesheet" href="/assets/css/0.styles.6c0c5975.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/zh/" class="home-link router-link-active"><!----> <span class="site-name">VuePress 社区</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/en/tools/mergeable/" class="nav-link">
  English
</a></li><li class="dropdown-item"><!----> <a href="/zh/tools/mergeable/" class="nav-link router-link-exact-active router-link-active">
  简体中文
</a></li></ul></div></div> <a href="https://github.com/vuepress/vuepress-community" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/en/tools/mergeable/" class="nav-link">
  English
</a></li><li class="dropdown-item"><!----> <a href="/zh/tools/mergeable/" class="nav-link router-link-exact-active router-link-active">
  简体中文
</a></li></ul></div></div> <a href="https://github.com/vuepress/vuepress-community" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>插件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>工具</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/zh/tools/mergeable/" class="active sidebar-link">vuepress-mergeable</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#为什么我需要这个？" class="sidebar-link">为什么我需要这个？</a></li><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#解构合并" class="sidebar-link">解构合并</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#定义对象中不同属性的合并方式" class="sidebar-link">定义对象中不同属性的合并方式</a></li><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#定义数组中不同元素的合并方式" class="sidebar-link">定义数组中不同元素的合并方式</a></li><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#使用函数定义合并方式" class="sidebar-link">使用函数定义合并方式</a></li><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#解构你的选项" class="sidebar-link">解构你的选项</a></li></ul></li><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#使用方法" class="sidebar-link">使用方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/zh/tools/mergeable/#mergeable-plugin-mergeoptions-defaultoptions-plugin" class="sidebar-link">mergeable(plugin, mergeOptions?, defaultOptions?): plugin</a></li></ul></li></ul></li><li><a href="/zh/tools/types/" class="sidebar-link">vuepress-types</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="vuepress-mergeable"><a href="#vuepress-mergeable" class="header-anchor">#</a> vuepress-mergeable <a rel="noopener noreferrer" target="_blank" title="vuepress/vuepress-community" href="https://github.com/vuepress/vuepress-community" class="github-link"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 28 28"><path d="M14 0C6.27 0 0 6.43 0 14.36c0 6.34 4.01 11.72 9.57 13.62.7.13.96-.31.96-.69 0-.34-.01-1.24-.02-2.44-3.89.87-4.72-1.92-4.72-1.92-.64-1.66-1.55-2.1-1.55-2.1-1.27-.89.1-.87.1-.87 1.4.1 2.14 1.48 2.14 1.48 1.25 2.19 3.28 1.56 4.07 1.19.13-.93.49-1.56.89-1.92-3.11-.36-6.38-1.59-6.38-7.09 0-1.57.55-2.85 1.44-3.85-.14-.36-.62-1.82.14-3.8 0 0 1.18-.39 3.85 1.47a12.8 12.8 0 0 1 3.5-.48c1.19.01 2.39.16 3.5.48 2.67-1.86 3.85-1.47 3.85-1.47.76 1.98.28 3.44.14 3.8.9 1 1.44 2.28 1.44 3.85 0 5.51-3.27 6.73-6.39 7.08.5.44.95 1.32.95 2.66 0 1.92-.02 3.47-.02 3.94 0 .38.25.83.96.69C23.99 26.07 28 20.7 28 14.36 28 6.43 21.73 0 14 0z" fill-rule="evenodd" clip-rule="evenodd"></path></svg></a></h1> <p><code>vuepress-mergeable</code> 基于 <a href="https://github.com/Shigma/deconstruct-merge" target="_blank" rel="noopener noreferrer">deconstruct-merge<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，可以用于定制化 VuePress 插件选项合并的方式。</p> <h2 id="为什么我需要这个？"><a href="#为什么我需要这个？" class="header-anchor">#</a> 为什么我需要这个？</h2> <p>在 VuePress 中，一个插件可以被多次使用但仅会保留一个 <code>options</code>。如果你的插件被多处使用（比如既在 <code>config.js</code> 中被直接引用，又被插件和主题间接地使用），它们的 <code>options</code> 将会互相覆盖，从而导致各种未知的负面影响。这个工具可以帮助你创造一个可以按照你的喜好自动合并选项的插件。</p> <h2 id="解构合并"><a href="#解构合并" class="header-anchor">#</a> 解构合并</h2> <p>合并两个选项的方式有很多种：</p> <ul><li><code>override</code>：后面传入选项覆盖前面的</li> <li><code>assign</code>：使用 <code>Object.assign</code> 进行合并</li> <li><code>array</code>：将全部传入的选项依次放入一个数组</li> <li><code>flat</code>：将传入的选项放入一个数组并压平</li></ul> <h3 id="定义对象中不同属性的合并方式"><a href="#定义对象中不同属性的合并方式" class="header-anchor">#</a> 定义对象中不同属性的合并方式</h3> <p>如果某个选项是一个对象，其中不同的属性要使用不同的合并方式，你可以将选项本身也写成对象式：</p> <div class="language-js extra-class"><pre class="language-js"><code>mergeOptions <span class="token operator">=</span> <span class="token punctuation">{</span>
  foo<span class="token operator">:</span> <span class="token string">'array'</span><span class="token punctuation">,</span>
  bar<span class="token operator">:</span> <span class="token string">'flat'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><p>此时，<code>foo</code> 选项将采用数组进行合并，而 <code>bar</code> 选项则采用压平的数组。</p> <h3 id="定义数组中不同元素的合并方式"><a href="#定义数组中不同元素的合并方式" class="header-anchor">#</a> 定义数组中不同元素的合并方式</h3> <p>如果某个选项是一个数组，其中不同的元素要使用不同的合并方式，你可以将选项本身也写成数组式：</p> <div class="language-js extra-class"><pre class="language-js"><code>mergeOptions <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'override'</span><span class="token punctuation">,</span> <span class="token string">'assign'</span><span class="token punctuation">]</span>
</code></pre></div><p>此时，数组的第一个元素将使用覆盖的合并方式，而第二个元素则采用 <code>Object.assign</code> 进行合并。</p> <h3 id="使用函数定义合并方式"><a href="#使用函数定义合并方式" class="header-anchor">#</a> 使用函数定义合并方式</h3> <p>如果上面的 4 种合并策略都不满足你的需求，则你可以自己写一个函数来定义合并方式：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function-variable function">mergeOptions</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> oldValue</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> value <span class="token operator">+</span> oldValue
<span class="token punctuation">}</span>
</code></pre></div><p>当然，如果有好的想法，我们也欢迎你向我们提出 PR。</p> <h3 id="解构你的选项"><a href="#解构你的选项" class="header-anchor">#</a> 解构你的选项</h3> <p>上面的语法可以被递归地应用在你的选项中，这使得你可以精细地定义每一个细节的合并逻辑。下面是一个案例：</p> <div class="language-js extra-class"><pre class="language-js"><code>mergeOptions <span class="token operator">=</span> <span class="token punctuation">{</span>
  foo<span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token string">'override'</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
      <span class="token function-variable function">bar</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> oldValue</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> value <span class="token operator">+</span> oldValue<span class="token punctuation">,</span>
      baz<span class="token operator">:</span> <span class="token string">'flat'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token string">'assign'</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="custom-block tip"><p class="custom-block-title">提示</p> <p><code>override</code> 是我们的默认合并策略。因此任何没有在 <code>mergeOptions</code> 中定义的对象属性或数组元素都将自动采取覆盖策略。</p></div> <h2 id="使用方法"><a href="#使用方法" class="header-anchor">#</a> 使用方法</h2> <h3 id="mergeable-plugin-mergeoptions-defaultoptions-plugin"><a href="#mergeable-plugin-mergeoptions-defaultoptions-plugin" class="header-anchor">#</a> mergeable(plugin, mergeOptions?, defaultOptions?): plugin</h3> <ul><li><strong>plugin:</strong> 任何 VuePress 插件</li> <li><strong>mergeOptions:</strong> 解构合并的选项</li> <li><strong>defaultOptions:</strong> 插件的默认选项</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// vuepress-plugin-my-plugin/index.js</span>
<span class="token keyword">const</span> mergeable <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'vuepress-mergeable'</span><span class="token punctuation">)</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token function">mergeable</span><span class="token punctuation">(</span>
  <span class="token parameter">options</span> <span class="token operator">=&gt;</span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token function">ready</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>options<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    <span class="token comment">// 解构合并的选项</span>
    foo<span class="token operator">:</span> <span class="token string">'assign'</span><span class="token punctuation">,</span>
    bar<span class="token operator">:</span> <span class="token string">'flat'</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    <span class="token comment">// 插件的默认选项</span>
    foo<span class="token operator">:</span> <span class="token punctuation">{</span> a<span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">)</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// .vuepress/config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  plugins<span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token comment">// 多次使用的你的插件</span>
    <span class="token punctuation">[</span>
      <span class="token string">'my-plugin'</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span>
        foo<span class="token operator">:</span> <span class="token punctuation">{</span> b<span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> c<span class="token operator">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
        baz<span class="token operator">:</span> <span class="token string">'Hello'</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span>
      <span class="token string">'my-plugin'</span><span class="token punctuation">,</span>
      <span class="token punctuation">{</span>
        foo<span class="token operator">:</span> <span class="token punctuation">{</span> b<span class="token operator">:</span> <span class="token number">3</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
        bar<span class="token operator">:</span> <span class="token string">'Hello'</span><span class="token punctuation">,</span>
        baz<span class="token operator">:</span> <span class="token string">'World'</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre></div><p>这会是得到的结果：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token punctuation">{</span> foo<span class="token operator">:</span> <span class="token punctuation">{</span> a<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> b<span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span> c<span class="token operator">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  bar<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'Hello'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  baz<span class="token operator">:</span> <span class="token string">'World'</span> <span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/vuepress/vuepress-community/edit/master/packages/docs/src/zh/tools/mergeable.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2019/10/15 上午7:51:18</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/zh/plugins/zooming/" class="prev">
        vuepress-plugin-zooming
      </a></span> <span class="next"><a href="/zh/tools/types/">
        vuepress-types
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.642d23b3.js" defer></script><script src="/assets/js/40.20d66e6f.js" defer></script><script src="/assets/js/page-5b4049d8.28b01370.js" defer></script><script src="/assets/js/42.2c37f203.js" defer></script>
  </body>
</html>
